/* License Notice:
**
** This program is free software: you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation, either version 3 of the License, or
**    (at your option) any later version.
** This program is distributed in the hope that it will be useful,
**   but WITHOUT ANY WARRANTY; without even the implied warranty of
**   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
**   GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
**   along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/**
 * @file time_keep.h
 * @author TooOld2Rock'nRoll
 * @date 2023/08/23
 * @brief Set of helped function to keep track of time.
 *
 * @todo implemente a simple timer for timeout operations: https://www.fluentcpp.com/2018/12/28/timer-cpp/
 */
#ifndef _TIME_KEEP_H_
#define _TIME_KEEP_H_

/*---- Includes ----*/
#include <stdbool.h>
#include <time.h>
#include <SDL2/SDL_timer.h>


#ifdef __cplusplus
extern "C" {
#endif
/*---- Typedefs ----*/


/*---- Enumerations ----*/


/*---- Structures ----*/
/** @brief Keeps track of elapsed time from specific time stamps. */
typedef struct _time_keep_
{
    double d_delta_t; ///<The elapsed time in seconds.
    uint64_t ui64_last_t; ///<The reference time stamp. 
    uint64_t ui64_current_t; ///<The most recent time reading.
} time_keep_s;


/*---- Macros ----*/


/*---- Function Prototypes ----*/
/**
 * @brief Gets Now in seconds from a high resolution timer.
 */
double getHiResTime ();

/**
 * @brief Sleeps the thread for the given time amount in seconds (nanosecond precision!).
 *
 * @param time - time to sleep in seconds.
 */
void sleep (double time);

/**
 * @brief Keeps track of the time between loop iterations.
 * @remarks The returned value will be unreliable until the first reset is done!
 *
 * @param keep - reference to a time keep structure.
 * @param reset - goes back to zero when <b>true</b>.
 *
 * @return Time elapsed in seconds (double) from the last time reset = true was called.
 * @retval 0 - if keep is null.
 */
double getDeltaTime (time_keep_s *keep, bool reset);

#ifdef __cplusplus
} //extern "C"
#endif
#endif /* _TIME_KEEP_H_ */

